﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Models;
using System.Data;


public partial class TagList : AdminBase
{

    private DataTable dc = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        grdbind();
    }

    public void grdbind()
    {
        using (Entities bll = new Entities())
        {
            var Bpl = bll.S_Goods.OrderBy(p => p.GID).ThenBy(p => p.GSort);
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = Bpl.ToList();
            pds.AllowPaging = true;
            AspNetPager1.RecordCount = Bpl.Count(); //记录总数；
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            pds.PageSize = AspNetPager1.PageSize;
            this.rptList.DataSource = pds;
            this.rptList.DataBind();
        }
    }

    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {   //当页面页数改变时，执行这个方法；
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        grdbind();
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = Convert.ToInt32(((Label)e.Item.FindControl("lb_id")).Text);
        Entities bll = new Entities();
        S_Goods model = bll.S_Goods.First(p => p.GID == id);
        switch (e.CommandName.ToLower())
        {
            case "lock":
                if (model.IsLock == true)
                {
                    model.IsLock = false;
                }
                else
                {
                    model.IsLock = true;
                }
                break;
            case "ibtnred":
                if (model.IsRed == true)
                {
                    model.IsRed = false;
                }
                else
                {
                    model.IsRed = true;
                }
                break;
            case "del":
                bll.S_Goods.Remove(model);
                break;
        }
        bll.SaveChanges();
        grdbind();

    }

    //改变状态点击事件
    public void change_Click(object sender, EventArgs e)
    {
        Entities db = new Entities();
        LinkButton lb = (LinkButton)sender;
        if (lb.CommandName == "lock")
        {
            int _Id = Convert.ToInt32(lb.CommandArgument);
            var bll = db.S_Goods.SingleOrDefault(p => p.GID == _Id);
            if (bll != null)
            {
                if (bll.IsLock == true)
                {
                    bll.IsLock = false;
                }
                else
                {
                    bll.IsLock = true;
                }
                db.SaveChanges();
            }
        }
    }

    //获取状态
    public string getState(bool Sta)
    {
        string result = "";
        if (Sta == true)
        {
            result = "<font color=red>锁定</font>";
        }
        else
        {
            result = "正常";
        }
        return result;
    }

    //最高级已经预定
    public static int GetISyuding(string CodeId)
    {
        int id = int.Parse(CodeId);

        int result =0;
        Entities db = new Entities();

            result = Convert.ToInt32(db.Y_Order.Where(x =>x.SPID== id && x.OState >= 0 && x.UparentId == "8888").Sum(t => t.ONumber));

        return result;
    }

    //已经预定
    public static int GetISTh(string CodeId)
    {
        int id = int.Parse(CodeId);

        int result = 0;
        Entities db = new Entities();

        result = Convert.ToInt32(db.S_Order.Where(x => x.SPID == id && x.OState >= 0).Sum(t => t.ONumber));

        return result;
    }

    //已经预定
    public static int GetISfh(string CodeId)
    {
        int id = int.Parse(CodeId);

        int result = 0;
        Entities db = new Entities();

        result = Convert.ToInt32(db.S_Order.Where(x => x.SPID == id && x.OState==2).Sum(t => t.ONumber));

        return result;
    }
    


}

